package com.yiwei.jmx.activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.IncomeHouseBean;
import com.yiwei.jmx.api.bean.UploadImgBean;
import com.yiwei.jmx.api.model.HouseCertConfModel;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.GlideRoundTransform;
import com.yiwei.jmx.util.ImageUtil;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.UI;
import com.yiwei.jmx.util.Util;
import com.yiwei.jmx.view.SelectAreaDilaog;
import com.yiwei.jmx.view.SelectMenuDilaog;
import com.yiwei.jmx.view.SelectMenuTipsDilaog;
import com.yiwei.jmx.view.V19FrameLayout;
import com.yiwei.jmx.view.uploadImage.UploadImgAdapter;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class HouseIdentityActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.edit_realname)
    EditText editRealname;
    @BindView(R.id.edit_idcard_no)
    EditText editIdcardNo;
    @BindView(R.id.btn_xingzhi)
    TextView btnXingzhi;
    @BindView(R.id.btn_get_way)
    TextView btnGetWay;
    @BindView(R.id.btn_address)
    TextView btnAddress;
    @BindView(R.id.edit_area)
    EditText editArea;
    @BindView(R.id.btn_example)
    TextView btnExample;
    @BindView(R.id.img_photo)
    ImageView imgPhoto;
    @BindView(R.id.btn_take_photo)
    ImageView btnTakePhoto;
    @BindView(R.id.btn_retake_photo)
    SuperButton btnRetakePhoto;
    @BindView(R.id.btn_submit)
    SuperButton btnSubmit;
    private Unbinder unbiner;
    private SelectMenuTipsDilaog selectMenuTipsDilaog;
    private SelectMenuDilaog selectMenuDilaog;
    private UploadImgAdapter uploadImgAdapter;
    private int selectPoi;
    private int take_type = 0;
    private boolean is_one_ok = false;
    private boolean is_two_ok = false;
    private String one_url;
    private String two_url;
    private IncomeHouseBean submitBean;
    private HouseCertConfModel.DataBean totalData;
    private SelectAreaDilaog selectAreaDilaog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_house_identity);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("房产认证");
        submitBean = new IncomeHouseBean();

        selectMenuDilaog = new SelectMenuDilaog(this);
        selectMenuDilaog.setOnChooseListener(new SelectMenuDilaog.OnChooseListener() {
            @Override
            public void onSuccess(int poi, String text, String menuType) {
                if (menuType.equals("nature")) {
                    submitBean.nature = text;
                    btnXingzhi.setText(text);
                } else if (menuType.equals("way")) {
                    submitBean.way = text;
                    btnGetWay.setText(text);
                }
            }
        });

        selectMenuTipsDilaog = new SelectMenuTipsDilaog(this).setOnChooseListener(new SelectMenuTipsDilaog.OnChooseListener() {
            @Override
            public void onLeft() {
                finish();
            }

            @Override
            public void onRight() {
                selectMenuTipsDilaog.dismiss();
            }
        }).showTwoSelectData("主人", R.mipmap.img_idcard_fail_dialog, "只有认证成功才能获取更高智信分您确定要离开吗？", "残忍离开", "我要留下");

        selectAreaDilaog = new SelectAreaDilaog(this).setOnChooseListener(new SelectAreaDilaog.OnChooseListener() {
            @Override
            public void onAddressSelect(String provinceId, String provinceName, String cityId, String cityName, String areaId, String areaName) {
                if (TextUtils.isEmpty(provinceName)) {
                    provinceName = "";
                }
                if (TextUtils.isEmpty(provinceName)) {
                    cityName = "";
                }
                if (TextUtils.isEmpty(provinceName)) {
                    areaName = "";
                }
                btnAddress.setText(provinceName + cityName + areaName);
                submitBean.province_id = provinceId;
                submitBean.city_id = cityId;
                submitBean.district_id = areaId;
            }
        });
        getIncomeCertConf();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    public void getUploadImage(Bitmap bitmap) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        loadingDialog.show();
        UploadImgBean bean = new UploadImgBean();
        bean.img = ImageUtil.Bitmap2StrByBase64(bitmap, 100);
        Api.api_service.getUploadImage(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (selectPoi == 0) {
                        Glide.with(this).load(result.url).into(imgPhoto);
                        submitBean.image = result.getUrl();
                    }
                });
    }

    private void getIncomeHouse() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getIncomeHouseV3(submitBean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    ToastUtil.makeText(this, "认证成功");
                    finish();
                });
    }

    private void getIncomeCertConf() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();

        Api.api_service.getHouseCertConf().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    totalData = result.data;
                });
    }

    private void initUploadImgAdapter() {
        uploadImgAdapter = new UploadImgAdapter(this, new UploadImgAdapter.Action2() {
            @Override
            public void call(Bitmap file, int mode) {
                if (mode == UploadImgAdapter.MODE_IMG) {
                    getUploadImage(file);
                }
            }

            @Override
            public void callTake(File file, int mode) {

            }
        }, 1);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (uploadImgAdapter != null) {
            uploadImgAdapter.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onBackPressed() {
        selectMenuTipsDilaog.show();
    }

    @OnClick({R.id.btn_back, R.id.btn_address, R.id.btn_example, R.id.btn_take_photo, R.id.btn_retake_photo, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                selectMenuTipsDilaog.show();
                break;
            case R.id.btn_address:
                selectAreaDilaog.show();
                break;

            case R.id.btn_example:
                Util.startActivity(this, HouseExampleActivity.class);
                break;
            case R.id.btn_take_photo:
            case R.id.btn_retake_photo:
                selectPoi = 0;
                checkPermission(new CheckPermListener() {
                    @Override
                    public void superPermission() {
                        if (uploadImgAdapter == null) {
                            initUploadImgAdapter();
                        }
                        uploadImgAdapter.withMode(UploadImgAdapter.MODE_IMG).showDialog();
                    }

                    @Override
                    public void onReject() {

                    }
                }, R.string.camera_storage_card_p, R.string.get_cammra, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE);
                break;
            case R.id.btn_submit:
                submitBean.property_name = UI.toString(editRealname);
                submitBean.idno = UI.toString(editIdcardNo);
                submitBean.address = UI.toString(editArea);

                if (submitBean.checkAll()) {
                    getIncomeHouse();
                }

                break;
        }
    }

    @OnClick({R.id.btn_xingzhi, R.id.btn_get_way, R.id.btn_address})
    public void onViewClicked1(View view) {
        switch (view.getId()) {
            case R.id.btn_xingzhi:
                if (totalData == null) {
                    getIncomeCertConf();
                } else {
                    selectMenuDilaog.setData("nature", totalData.nature);
                    selectMenuDilaog.show();
                }
                break;
            case R.id.btn_get_way:
                if (totalData == null) {
                    getIncomeCertConf();
                } else {
                    selectMenuDilaog.setData("way", totalData.way);
                    selectMenuDilaog.show();
                }
                break;
            case R.id.btn_address:
                selectAreaDilaog.show();
                break;
        }
    }
}
